<?php
/**
 * Created by EditPlus.
 * User: GRAY <iamgray@gmail.com>
 * Date: 8/4/16
 * Time: 19:43
 */

namespace Home\Controller;

use Common\Library\BusinessException;
use Common\Library\Constant;
use Think\Controller;
use Common\Library\Http\Request;
use Common\Library\Http\Response;
use Think\Log;
use Common\Vendor\Hook\BaseHook;


class HulushujuController extends Controller {

    public function __construct() {
        parent::__construct();

        try {
            Request::parse();
        } catch (BusinessException $e) {
            $this->ajaxReturn(['code' => $e->getCode(), 'msg' => $e->getMessage()]);
        }
    }

    // 获取token
    public function getToken() {
        $url = 'https://data.hulushuju.com/api/companies/huoyanzichan_CRAWLER/access_token?signature=5fa167c3da6a44b0abda806c06ae3287';
        $json = file_get_contents($url);
        $data_arr = json_decode($json,true);
        if($data_arr['code_description']=='SUCCESS') {
            return $data_arr['data']['access_token'];
        } else {
             return false;
        }
    }
    //获取采集报告
    public function getReport() {
        $accessToken = $this->getToken();
        echo $accessToken;
        $token = $_GET['token'];
        if($accessToken && $token) {
            $url = "https://data.hulushuju.com/api/data/reports/$token?companyAccount=huoyanzichan_CRAWLER&accessToken=$accessToken";
            $json = file_get_contents($url);
            //$data_arr = json_decode($json,true);
            echo $json;
        }
    }
    //获取原始数据
    public function getRawdata() {

        $accessToken = $this->getToken();
        $token = $_GET['token'];
        if($accessToken && $token) {
            $url = "https://data.hulushuju.com/api/data/rawdatas/old/$token?companyAccount=huoyanzichan_CRAWLER&accessToken=$accessToken";
            $json = file_get_contents($url);
            //$data_arr = json_decode($json,true);
            echo $json;
        }
    }

    //获取采集报告通话记录
    public function getReportCalls() {

        $accessToken = $this->getToken();
        $token = $_GET['token'];
        if($accessToken && $token) {
            $url = "https://data.hulushuju.com/api/data/reports/$token?companyAccount=huoyanzichan_CRAWLER&accessToken=$accessToken";
            $json = file_get_contents($url);
            $data_arr = json_decode($json,true);
            if($data_arr) {
                if($data_arr['code_description']=='DATA_REPORT_ACCESS_SUCCESS') {
                    //echo '<pre>';
                    //print_r($data_arr['data']['contact_list']);
                    echo json_encode($data_arr['data']['contact_list']);
                }
            }
        }
    }

    //获取采集报告风险点
    public function getReportBehavior() {

        $accessToken = $this->getToken();
        $token = $_GET['token'];
        if($accessToken && $token) {
            $url = "https://data.hulushuju.com/api/data/reports/$token?companyAccount=huoyanzichan_CRAWLER&accessToken=$accessToken";
            $json = file_get_contents($url);
            $data_arr = json_decode($json,true);
            if($data_arr) {
                if($data_arr['code_description']=='DATA_REPORT_ACCESS_SUCCESS') {
                    //echo '<pre>';
                    //print_r($data_arr['data']['contact_list']);
                    echo json_encode($data_arr['data']['behavior_check']);
                }
            }
        }
    }
    //获取原始数据通话记录
    public function getRawdataCalls() {

        $accessToken = $this->getToken();
        $token = $_GET['token'];
        if($accessToken && $token) {
            $url = "https://data.hulushuju.com/api/data/rawdatas/old/$token?companyAccount=huoyanzichan_CRAWLER&accessToken=$accessToken";
            $json = file_get_contents($url);
            $data_arr = json_decode($json,true);
            if($data_arr) {
                if($data_arr['code_description']=='DATA_RAW_ACCESS_SUCCESS') {
                    //echo '<pre>';
                    //print_r($data_arr['data'][0]['calls']);
                    echo json_encode($data_arr['data'][0]['calls']);
                }
            }
        }
    }
}
